import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/auth/Login.vue')
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('../views/auth/Register.vue'),
      meta: { 
        clearForm: true // 标记需要清空表单的路由
      }
    },
    {
      path: '/user',
      name: 'UserCenter',
      component: () => import('../views/user/UserCenter.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/favorites',
      name: 'Favorites',
      component: () => import('../views/user/Favorites.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/attractions',
      name: 'Attractions',
      component: () => import('../views/Attractions.vue')
    },
    {
      path: '/attraction/:id',
      name: 'AttractionDetail',
      component: () => import('../views/AttractionDetail.vue')
    },
    {
      path: '/about',
      name: 'About',
      component: () => import('../views/About.vue')
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('../views/NotFound.vue')
    },
    {
      path: '/admin',
      component: () => import('../views/admin/AdminLayout.vue'),
      meta: { requiresAuth: true, requiresAdmin: true },
      children: [
        {
          path: '',
          name: 'AdminDashboard',
          component: () => import('../views/admin/Dashboard.vue')
        },
        {
          path: 'users',
          name: 'UserManagement',
          component: () => import('../views/admin/UserManagement.vue')
        },
        {
          path: 'categories',
          name: 'CategoryManagement',
          component: () => import('../views/admin/CategoryManagement.vue')
        },
        {
          path: 'attractions',
          name: 'AttractionManagement',
          component: () => import('../views/admin/AttractionManagement.vue')
        },
        {
          path: 'attractions/create',
          name: 'CreateAttraction',
          component: () => import('../views/admin/CreateAttraction.vue')
        },
        {
          path: 'attractions/edit/:id',
          name: 'EditAttraction',
          component: () => import('../views/admin/EditAttraction.vue')
        },
        {
          path: 'comments',
          name: 'CommentManagement',
          component: () => import('../views/admin/CommentManagement.vue')
        },
        {
          path: 'advertisements',
          name: 'AdvertisementManagement',
          component: () => import('../views/admin/AdvertisementManagement.vue')
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('token'); // 简单判断是否登录
  const userRole = JSON.parse(localStorage.getItem('user'))?.role || '';
  
  if (to.matched.some(record => record.meta.requiresAuth) && !isLoggedIn) {
    next({ 
      name: 'Login', 
      query: { redirect: to.fullPath },
      params: { message: '请先登录后再访问该页面' }
    });
  } else if (to.matched.some(record => record.meta.requiresAdmin) && userRole !== 'admin') {
    // 修改这里的判断条件，使用小写的"admin"
    next({ 
      name: 'Home',
      params: { message: '您没有权限访问该页面' }
    });
  } else {
    next();
  }
})

export default router 